package Loganseabolt.blocks;

import java.util.ArrayList;
import java.util.Random;

import Loganseabolt.LoganSeaboltModBase;
import Loganseabolt.util;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.gui.ChatLine;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityGiantZombie;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;


public class Mob_Spawners extends Block  {

	public Mob_Spawners(int par1, Material par2Material) {
		super(par1, par2Material);
		this.setBlockUnbreakable();
		this.setCreativeTab(CreativeTabs.tabBlock);
		// TODO Auto-generated constructor stub
	}
	public void registerIcons(IconRegister iconReg)
    {
        blockIcon = iconReg.registerIcon("LoganSeaboltModBase:Spawner");
    }
	 
	
	 public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
	 {
		 if (!par1World.isRemote)
		 {
			 if (Activated)
				 return true;
			 Activated = true;
			 par1World.scheduleBlockUpdate(par2, par3, par4, blockID, 10);
		 }
		 return true;
	 }
	
	@Override
	public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
	 {
		 if (Activated)
		 {
			 if (maxNumCreatures > allLiving.size() )
			 {
				 if (numsZombieCreated < numsZombie)
				 {
					 EntityZombie Zambi = (EntityZombie)new EntityZombie(par1World);
					 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
					 Zambi.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
					 par1World.spawnEntityInWorld(Zambi);
					 Zambi.setCurrentItemOrArmor(0, new ItemStack(Item.pickaxeIron));
					 Zambi.setCurrentItemOrArmor(1, new ItemStack(Item.bootsLeather));
					 Zambi.setCurrentItemOrArmor(2, new ItemStack(Item.legsLeather));
					 Zambi.setCurrentItemOrArmor(3, new ItemStack(Item.plateLeather));
					 ItemStack LeatherH= new ItemStack(Item.helmetLeather, 1);
					 Zambi.setCurrentItemOrArmor(4, LeatherH);
					 numsZombieCreated++;
					 allLiving.add(Zambi);
				 }
				 else if (numsBlazeCreated < numsBlaze)
				 {
					 EntityBlaze Blazen = (EntityBlaze)new EntityBlaze(par1World);
					 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
					 Blazen.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
					 par1World.spawnEntityInWorld(Blazen);
					 numsBlazeCreated++;
					 allLiving.add(Blazen);
				 }
				 else if (numsSpiderCreated < numsSpider)
				 {

					 EntitySpider Spidy = (EntitySpider)new EntitySpider(par1World);
					 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
					 Spidy.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
					 par1World.spawnEntityInWorld(Spidy);
					 Spidy.setCurrentItemOrArmor(4, new ItemStack(Item.helmetLeather));
					 numsSpiderCreated++;
					 allLiving.add(Spidy);
				 }
				 else if (numsWitchCreated < numsWitch)
				 {

					 EntityWitch Witch = (EntityWitch)new EntityWitch(par1World);
					 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
					 Witch.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
					 par1World.spawnEntityInWorld(Witch);
					 numsWitchCreated++;
					 allLiving.add(Witch);
				 }
				 else if (numsSkeltonCreated < numsSkelton)
				 {
					 EntitySkeleton skeley = (EntitySkeleton)new EntitySkeleton(par1World);
					 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
					 skeley.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
					 par1World.spawnEntityInWorld(skeley);
					 skeley.setCurrentItemOrArmor(0, new ItemStack(Item.bow));
					 numsSkeltonCreated++;
					 allLiving.add(skeley);
				 }
				 else if (getMaxToCreate() -1 == getAllCrated() && allLiving.size() == 0)
				 {
					 if (round == 5)
					 {
						 EntityGiantZombie Boss = (EntityGiantZombie)new EntityGiantZombie(par1World);
						 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
						 Boss.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
						 par1World.spawnEntityInWorld(Boss);
						 ItemStack GS= new ItemStack(LoganSeaboltModBase.Grass_Sword);
						 Boss.setCurrentItemOrArmor(0, GS);
						 Boss.setCurrentItemOrArmor(1, new ItemStack(Item.bootsLeather));
						 Boss.setCurrentItemOrArmor(2, new ItemStack(Item.legsLeather));
						 Boss.setCurrentItemOrArmor(3, new ItemStack(Item.plateLeather));
						 ItemStack LeatherH= new ItemStack(Item.helmetLeather, 1);
						 Boss.setCurrentItemOrArmor(4, LeatherH);
						 numsBossCreated++;
						 allLiving.add(Boss);
					 }
					 else
					 {
						 EntityChicken chicky = (EntityChicken)new EntityChicken(par1World);
						 Vec3 skeyPos = util.placeInRandomDirection(par2,par3, par4, 10);
						 chicky.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
						 par1World.spawnEntityInWorld(chicky);
						 numsBossCreated++;
						 allLiving.add(chicky);
					 }
				 }
			 }
			 
			 for (EntityLiving living :allLiving)
			 {
				 if (living.isDead)
				 {
					 allLiving.remove(living);
					 living.dropItem(LoganSeaboltModBase.Souls.itemID, 3);
					 break;
				 }
			 }
			 int mod = 1;
			 if (getAllCrated() >= getMaxToCreate())
			 {
				mod = 5;
				 numsBlazeCreated = 0;
				 numsSkeltonCreated = 0;
				 numsSpiderCreated = 0;
				 numsWitchCreated = 0;
				 numsZombieCreated = 0;
				 numsBossCreated = 0;
				 numsBlaze++ ;
				 numsSkelton++;
				 numsSpider++ ;
				 numsZombie++ ;
				 round++;
			 }
			 par1World.scheduleBlockUpdate(par2, par3, par4, blockID, 60 * mod);
			
			 }
	 }
	protected int getAllCrated()
	{
		return numsBlazeCreated+numsWitchCreated+numsSkeltonCreated+numsZombieCreated+numsSpiderCreated +numsBossCreated;
		
	}
	protected int getMaxToCreate()
	{
		return numsBlaze+numsWitch+numsSkelton+numsZombie+numsSpider+numsBoss;
	}
	private int round = 1;
	private int numsBossCreated = 0;
	private int numsWitchCreated = 0;
	private int numsZombieCreated = 0;
	private int numsSkeltonCreated = 0;
	private int numsSpiderCreated = 0;
	private int numsBlazeCreated = 0;
	private static int numsBoss = 1;
	private static int numsBlaze = 2;
	private static int numsWitch = 2;
	private static int numsZombie = 5;
	private static int numsSkelton = 3;
	private static int numsSpider = 5;
	private static int maxNumCreatures = 20;
	private ArrayList<EntityZombie> ZL= new ArrayList();
	private ArrayList<EntitySkeleton> SKL= new ArrayList();
	private ArrayList<EntitySpider> SPL= new ArrayList();
	private ArrayList<EntityZombie> WL= new ArrayList();
	private ArrayList<EntityLiving> allLiving = new ArrayList<EntityLiving>();
	private boolean Activated = false;
}
/* EntityPig skeley = (EntityPig)new EntityPig(par2World);
EntityCow cow = (EntityCow)new EntityCow(par2World);
EntityChicken Chicky = (EntityChicken)new EntityChicken(par2World);
skeley.setPosition(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ);
skeley.rotationYaw = par3EntityPlayer.rotationYaw - 90;
skeley.posX -= ((double)(MathHelper.cos(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;
skeley.posZ -= ((double)(MathHelper.sin(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;

Vec3 skeyPos = util.placeInFrontOfEntity(par3EntityPlayer, 10);
Vec3 cowPos = util.placeInFrontOfEntity(par3EntityPlayer, 11);
Vec3 chickPos = util.placeInFrontOfEntity(par3EntityPlayer, 12);
skeley.setPosition(skeyPos.xCoord, skeyPos.yCoord, skeyPos.zCoord);
cow.setPosition(cowPos.xCoord, cowPos.yCoord, cowPos.zCoord);
Chicky.setPosition(chickPos.xCoord, chickPos.yCoord, chickPos.zCoord);
par2World.spawnEntityInWorld(skeley);
par2World.spawnEntityInWorld(cow);
par2World.spawnEntityInWorld(Chicky);
*/